<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;

class SessionsController extends Controller{
    public function __construct(){
        $this->middleware('guest',[
            'only' => ['create']
        ]);
    }
    // 登陆页面
    public function create(){
        return view('sessions.create');
    }
    //  登陆逻辑
    public function store(Request $request){
        $credentials = $this->validate($request,[
            'email' => 'required|email|max:255',
            'password' => 'required',
        ]);

        if(Auth::attempt($credentials,$request->has('remember'))){
            $user = Auth::user();
            if($user->activated){
                # 登陆成功
                session()->flash('success','欢迎回来!');
                // return redirect()->route('users.show',['user'=>Auth::id()]);
                return redirect()->intended(route('users.show',['user'=>$user]));
            }else{
                Auth::logout();
                session()->flash('warning','你的账号未激活，请检查邮箱中的注册邮件进行激活');
                return redirect('/');
            }
        }else{
            # 登陆失败
            session()->flash('danger','很抱歉，您的邮箱和密码不匹配');
            return redirect()->back()->withInput();
        }
    }

    public function destroy(){
        Auth::logout();
        session()->flash('success','您已成功退出');
        return redirect('login');
    }
}
